//
//  IGServerConfig.m
//  jingdongfang
//
//  Created by BOE on 2017/8/9.
//  Copyright © 2017年 ZhiYu Hao. All rights reserved.
//

#import "IGServerConfig.h"

typedef NS_ENUM(NSInteger, IGServerConfigType) {
    IGServerConfigTypeSit = 0,                      ///<sit环境
    IGServerConfigTypePre,                          ///<pre环境
    IGServerConfigTypeProd                          ///<prod生产环境
};

@interface IGServerConfig()

@property (nonatomic, strong) NSString *apiBaseUrl;           ///< api基地址
@property (nonatomic, strong) NSString *sitBaseUrl;           ///< sit基地址
@property (nonatomic, strong) NSString *preBaseUrl;           ///< pre基地址
@property (nonatomic, strong) NSString *prodBaseUrl;          ///< prod生产环境基地址


@end

@implementation IGServerConfig

+ (instancetype)sharedConfig {
    static IGServerConfig *sharedConfig;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedConfig = [[IGServerConfig alloc] init];
    });
    return sharedConfig;
}

- (instancetype)init {
    if (self = [super init]) {
        [self loadServerPlist];
    }
    return self;
}

- (long long)cacheVersion {
    return [[[self infoDictionary] objectForKey:@"CFBundleVersion"] longLongValue];
}

- (NSString *)appVersion {
    return [[self infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}

- (NSString *)shortVersion {
    return [[self infoDictionary] objectForKey:@"CFBundleVersion"];
}

- (NSString *)appkey {
    return @"1.0.1";
}

/// Yes：Debug地址，NO：正式地址
- (BOOL)isDebug {
//    #ifdef DEBUG
        return YES;
//    #else
//        return NO;
//    #endif
}


- (IGServerConfigType)serverType {
    return IGServerConfigTypeSit;
//    return IGServerConfigTypePre;
//    return IGServerConfigTypeProd;
}

- (NSString *)httpApiBaseUrl {
    return [NSString stringWithFormat:@"http://%@", [IGServerConfig sharedConfig].apiBaseUrl];
}

- (NSString *)httpsApiBaseUrl {
    return [NSString stringWithFormat:@"https://%@", [IGServerConfig sharedConfig].apiBaseUrl];
}

#pragma mark -

- (void)loadServerPlist {
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"IGServerConfig" ofType:@"plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filepath];
    self.sitBaseUrl = dict[@"sitBaseUrl"];
    self.preBaseUrl = dict[@"preBaseUrl"];
    self.prodBaseUrl = dict[@"prodBaseUrl"];

    if ([self isDebug]) {//Debug环境
        self.apiBaseUrl = self.sitBaseUrl;
        
        if ([self serverType] == IGServerConfigTypeSit) {
            self.apiBaseUrl = self.sitBaseUrl;
        } else if ([self serverType] == IGServerConfigTypePre) {
            self.apiBaseUrl = self.preBaseUrl;
        }
    } else {//正式环境
        self.apiBaseUrl = self.prodBaseUrl;
    }
}

- (NSDictionary *)infoDictionary
{
    static NSDictionary *_infoDictionary;
    if (!_infoDictionary) {
        _infoDictionary = [[NSBundle mainBundle] infoDictionary];
    }
    return _infoDictionary;
}

@end
